export const usePayHook = () => {
    // 支付
    const wxPay = async (payInfo, msg = '支付', success = () => {
    }) => {
        // #ifdef MP-WEIXIN
        console.log("MP-WEIXIN-Pay", payInfo)
        await uni.requestPayment({
            provider: 'wxpay',
            timeStamp: payInfo.timeStamp,
            nonceStr: payInfo.nonceStr,
            package: payInfo.package,
            signType: payInfo.signType,
            paySign: payInfo.paySign,
            success(res) {
                console.log("MP-WEIXIN-Pay", res)
                uni.$emit('getUserInfo')
                uni.showToast({
                    title: msg + '成功',
                    icon: 'none',
                    success: function () {
                        success()
                    },
                })
            },
            fail(err) {
                console.log("MP-WEIXIN-Pay", err)
                uni
                    .showToast({
                        title: msg + '取消',
                        icon: 'none',
                    })
                    .then(() => {
                    })
            },
        })
        // #endif
        // #ifdef APP-PLUS
        console.log("APP-PLUS-Pay", payInfo)
        uni.requestPayment({
            provider: "wxpay",
            orderInfo: payInfo,
            success(res) {
                console.log("APP-PLUS-Pay", res)
                uni.$emit('getUserInfo')
                uni.showToast({
                    title: msg + '成功',
                    icon: 'none',
                    success: function () {
                        success()
                    },
                })
            },
            fail(e) {
                console.log("APP-PLUS-Pay", e)
                uni
                    .showToast({
                        title: msg + '取消',
                        icon: 'none',
                    })
                    .then(() => {
                    })
            }
        })
        // #endif
        // #ifdef H5
        console.log("H5", payInfo)
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                appId: payInfo.appId,
                timeStamp: payInfo.timeStamp,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                signType: payInfo.signType,
                paySign: payInfo.paySign,
            },
            function (res) {
                console.log("H5 --- MSG 》 ", res)
                if (res.err_msg === "get_brand_wcpay_request:ok") {
                    uni.showToast({
                        title: '支付成功',
                        icon: 'none',
                        success: function () {
                            success()
                        }
                    });
                } else {
                    uni.showToast({
                            title: msg + '取消',
                            icon: 'none',
                        })
                        .then(() => {
                        })
                }
            });
        // #endif
    }
    const aliPay = async (payInfo, msg = '支付', success = () => {
    }) => {
        console.log("APP-PLUS-Pay", payInfo)
        uni.requestPayment({
            provider: "alipay",
            orderInfo: payInfo,
            success(res) {
                console.log("APP-PLUS-Pay", res)
                uni.$emit('getUserInfo')
                uni.showToast({
                    title: msg + '成功',
                    icon: 'none',
                    success: function () {
                        success()
                    },
                })
            },
            fail(e) {
                console.log("APP-PLUS-Pay", e)
                uni
                    .showToast({
                        title: msg + '失败',
                        icon: 'none',
                    })
                    .then(() => {
                    })
            }
        })
    }
    return {
        wxPay, aliPay
    }
}
